<?php

/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class GoodsController extends CommonController {

    protected $goods_id;
    protected $w_id;

    /**
     * 构造函数   加载user.php的语言包 并映射到模版
     */
    public function __construct() {
        parent::__construct();
        $this->goods_id = isset($_REQUEST ['id']) ? intval($_REQUEST ['id']) : 0;

		/*rg808 临时屏蔽 by cxy */
		$this->assign('w_id', $this->w_id);
		$this->assign('store_list', model('Goods')->get_storelist());
    }

    /**
     *  商品详情页
     */
    public function index() {

        // 获得商品的信息
        $goods = model('Goods')->get_goods_info($this->goods_id);
        // 如果没有找到任何记录则跳回到首页
        if ($goods === false) {
            ecs_header("Location: ./\n");
        } else {
			
			$this->assign('fs_user_rand', $this->model->table('users')->field('user_rank')->where("user_id='".$_SESSION['user_id']."'")->getOne());

            if ($goods ['brand_id'] > 0) {
                $goods ['goods_brand_url'] = url('brand/index', array('id' => $goods ['brand_id']));
            }
            $shop_price = $goods ['shop_price'];
            $linked_goods = model('Goods')->get_related_goods($this->goods_id);
            $goods ['goods_style_name'] = add_style($goods ['goods_name'], $goods ['goods_name_style']);
			
            // 购买该商品可以得到多少钱的红包
            if ($goods ['bonus_type_id'] > 0) {
                $time = gmtime();
                $condition = "type_id = '$goods[bonus_type_id]' " . " AND send_type = '" . SEND_BY_GOODS . "' " . " AND send_start_date <= '$time'" . " AND send_end_date >= '$time'";
                $count = $this->model->table('bonus_type')->field('type_money')->where($condition)->getOne();

                $goods ['bonus_money'] = floatval($count);
                if ($goods ['bonus_money'] > 0) {
                    $goods ['bonus_money'] = price_format($goods ['bonus_money']);
                }
            }
            $goods ['comment_count'] = model('Goods')->get_record_count($this->goods_id);
            $this->assign('goods', $goods);
            $this->assign('goods_id', $goods ['goods_id']);
            $this->assign('promote_end_time', $goods ['gmt_end_time']);
            // 获得商品的规格和属性
            $properties = model('Goods')->get_goods_properties($this->goods_id);
            // 商品属性
            $this->assign('properties', $properties ['pro']);
            // 商品规格
            $this->assign('specification', $properties ['spe']);
            // 相同属性的关联商品
            $this->assign('attribute_linked', model('Goods')->get_same_attribute_goods($properties));
            // 关联商品
            $this->assign('related_goods', $linked_goods);
            // 关联文章
            $this->assign('goods_article_list', model('Goods')->get_linked_articles($this->goods_id));
            // 配件
            $this->assign('fittings', model('Goods')->get_goods_fittings(array($this->goods_id)));
            // 会员等级价格
            $this->assign('rank_prices', model('Goods')->get_user_rank_prices($this->goods_id, $shop_price));
            // 商品相册
            $this->assign('pictures', model('GoodsBase')->get_goods_gallery($this->goods_id));
            // 获取关联礼包
            $package_goods_list = model('Goods')->get_package_goods_list($goods ['goods_id']);
            $this->assign('package_goods_list', $package_goods_list);
            //取得商品优惠价格列表
            $volume_price_list = model('GoodsBase')->get_volume_price_list($goods ['goods_id'], '1');
            // 商品优惠价格区间
            $this->assign('volume_price_list', $volume_price_list);
        }

        // 检查是否已经存在于用户的收藏夹
        if ($_SESSION ['user_id']) {
            $where['user_id'] = $_SESSION ['user_id'];
            $where['goods_id'] = $this->goods_id;
            $rs = $this->model->table('collect_goods')->where($where)->count();
            if ($rs > 0) {
                $this->assign('sc', 1);
            }
        }

        /* 记录浏览历史 */
        if (!empty($_COOKIE ['ECS'] ['history'])) {
            $history = explode(',', $_COOKIE ['ECS'] ['history']);
            array_unshift($history, $this->goods_id);
            $history = array_unique($history);
            while (count($history) > C('history_number')) {
                array_pop($history);
            }
            setcookie('ECS[history]', implode(',', $history), gmtime() + 3600 * 24 * 30);
        } else {
            setcookie('ECS[history]', $this->goods_id, gmtime() + 3600 * 24 * 30);
        }
        // 更新点击次数
        $data = 'click_count = click_count + 1';
        $this->model->table('goods')->data($data)->where('goods_id = ' . $_REQUEST['id'])->update();
        // 当前系统时间
        $this->assign('now_time', gmtime());
        $this->assign('sales_count', model('GoodsBase')->get_sales_count($this->goods_id));
        $this->assign('image_width', C('image_width'));
        $this->assign('image_height', C('image_height'));
        $this->assign('id', $this->goods_id);
        $this->assign('type', 0);
        $this->assign('cfg', C('CFG'));
        // 促销信息
        $this->assign('promotion', model('GoodsBase')->get_promotion_info($this->goods_id));
        $this->assign('promotion_info', model('GoodsBase')->get_promotion_info());
        $this->assign('title', L('goods_detail'));
		/* 页面标题 */
        $page_info = get_page_title($goods['cat_id'], $goods['goods_name']);
        $this->assign('ur_here', $page_info['ur_here']);
        $this->assign('page_title', $page_info['title']);

		$this->assign('gift_list', model('Goods')->get_goods_fittings(array($this->goods_id), 1));
		$this->assign('addbuy_list', model('Goods')->get_goods_fittings(array($this->goods_id), 0, 1));

        $this->display('goods.dwt');
    }

    /**
     * 改变属性、数量时重新计算商品价格
     */
    public function price() {
        //格式化返回数组
        $res = array( 'err_msg' => '', 'result' => '', 'qty' => 1 );
        // 获取参数
        $attr_id = isset($_REQUEST ['attr']) ? explode(',', $_REQUEST ['attr']) : array();
        $number = (isset($_REQUEST ['number'])) ? intval($_REQUEST ['number']) : 1;
        // 如果商品id错误
        if ($this->goods_id == 0) {
            $res ['err_msg'] = L('err_change_attr');
            $res ['err_no'] = 1;
        } else {
            // 查询
            $condition = 'goods_id =' . $this->goods_id;
            $goods = $this->model->table('goods')->field('goods_name, shop_price, goods_number, extension_code')->where($condition)->find();

            if ($number <= 0) {
                $res ['qty'] = $number = 1;
            } else {
                $res ['qty'] = $number;
            }
            $shop_price		= model('GoodsBase')->get_final_price($this->goods_id, $number, true, $attr_id);
			$product_info	= model('ProductsBase')->get_products_info($this->goods_id, $attr_id);
            $res['shop_price']		= price_format($shop_price);
			$res['fs_shop_price']	= price_format($product_info['product_price'] ? $product_info['product_price'] : $goods['shop_price']);
            $res['result']			= price_format($shop_price * $number);
        }
        die(json_encode($res));
    }

	public function api_area(){
		$a_id = I('a_id'); $level = I('level');

		if(I('rg_ipLoc')){
			setcookie('rg_ipLoc', I('rg_ipLoc')); 

			$rg_arr = explode("-", I('rg_ipLoc'));
			$regions_data = $this->model->table('region')->where("region_id ='".$rg_arr[2]."'")->find();
			$top_id = $this->model->table('region')->field("parent_id")->where("region_id ='".$regions_data['parent_id']."'")->getOne();
			die(json_encode($top_id.'-'.$regions_data['parent_id'].'-'.$regions_data['region_id']));
		}else{
			$list = model('RegionBase')->get_regions($level, $a_id);
			die(json_encode($list));
		}
	}
	public function inventory(){
		$rg_goods	= model('Goods')->get_goods_info($this->goods_id);

		$attr_id	= isset($_REQUEST['attr']) ? explode(',', $_REQUEST['attr']) : array();

		$rg_count = $this->model->table('products')->field("count(*)")->where("goods_id = '$goods_id'")->getOne();
		if ($rg_count){
			$product_info = model('ProductsBase')->get_products_info($goods_id, $attr_id);
			$w_num = $product_info['product_number'] > 0 ? $product_info['product_number'] : 0;
		}else{
			$w_num = $rg_goods['goods_number'];
		}

		if($w_num > 5){
			$StockState = 1; $code = 0; $ArrivalDate = 0;
		}elseif($w_num > 0 && $w_num <= 5){
			$StockState = 2; $code = 0; $ArrivalDate = 0;
		}else{
			if (empty($attr_id)){
				if($rg_goods['goods_number'] > 0){
					$StockState = 1; $code = 0; $ArrivalDate = 0;
				}else{
					$StockState = 0; $code = 1; $ArrivalDate = 0;
				}
			}else{
				$StockState = 0; $code = 1; $ArrivalDate = 0;
			}
		}

		$store_num = model('Base')->get_store_num($this->goods_id, $attr_id);

		$rg_ajax = array('state'=>$StockState, 'code'=>$code, 'arrivalDate'=>$ArrivalDate, 'w_id'=>$this->w_id, 'w_num'=>$w_num, 'store_num'=>$store_num);

		die(json_encode($rg_ajax));
	}

	public function get_rg_info(){
		$rg_res['storelist']	= model('Base')->get_storelist($this->rg_arr);
		$rg_res['w_id']			= $this->w_id;

		die( json_encode($rg_res) );
	}

}